﻿#include "common.h" // 包含预编译头文件
#include "OpenPCMWindow.h"

OpenPCMWindow::OpenPCMWindow(QWidget *parent)
	: QWidget(parent)
	, sure(false)
	, fromSetting("")
{
	ui.setupUi(this);
	// 设置为独立窗口类型，确保可拖动
	setWindowFlags(Qt::Window);

	this->setWindowTitle("Open PCM File");
	//this->setEnabled(true);

	// 连接信号槽
	//connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(turnoff()));
	connect(ui.pushButtonConfirm, SIGNAL(clicked()), this, SLOT(confirm()));
	connect(ui.pushButtonTurnoff, SIGNAL(clicked()), this, SLOT(turnoff()));
}

OpenPCMWindow::~OpenPCMWindow()
{
}

void OpenPCMWindow::confirm()
{
	// 获取用户输入的 PCM 格式设置
	QString channels = ui.comboBox_channels->currentText();
	QString sampleRate = ui.comboBox_sample_rate->currentText();
	QString sampleFormat = ui.comboBox_format->currentText();
	fromSetting = "channels: " + channels + "    sample rate: " + sampleRate + "    sample format: " + sampleFormat;

#ifdef MUSICREADER_H
	if (sampleFormat == QString::fromUtf8(u8"8位 PCM （无符号）"))
		sampleFormat = "u8";
	else if (sampleFormat == QString::fromUtf8(u8"16位 PCM"))
		sampleFormat = "s16";
	else if (sampleFormat == QString::fromUtf8(u8"32位 PCM"))
		sampleFormat = "s32";
    else if (sampleFormat == QString::fromUtf8(u8"32位 IEEE 浮点"))
		sampleFormat = "f32";

	in_pcm_format.set_in_pcm_format(channels.toInt(),
		sampleRate.toInt(),
		sampleFormat.toUtf8().constData());
#endif

	// 关闭窗口，发送信号，传递设置
	sure = true;
	close();
}

void OpenPCMWindow::turnoff()
{
	// 关闭窗口，发送信号，传递设置
	sure = false;
	close();
}

void OpenPCMWindow::closeEvent(QCloseEvent* event)
{
	emit finished(sure, fromSetting);
}

